package com.lej.core.permission

import android.content.Context
import androidx.fragment.app.FragmentActivity
import com.lej.core.utils.KLog
import com.lej.core.utils.showInQueue
import com.lej.theme.dialog.CustomAlertPopDialog
import com.permissionx.guolindev.PermissionX
import kotlin.coroutines.resume
import kotlin.coroutines.suspendCoroutine

suspend fun PerInterface.checkWithReq(activity: FragmentActivity): Boolean {
    if (getNeedPermissions().all { PermissionX.isGranted(activity, it) }) {
        return true
    }
    val title = getTitle()
    val content = getContent()
    return suspendCoroutine<Boolean> {
        CustomAlertPopDialog.buildXPopup(
            activity,
            title,
            content = content,
            confirmText = "现在就要",
            cancelText = "再等等",
            onConfirm = {
                val startTime = System.currentTimeMillis()
                PermissionX.init(activity)
                    .permissions(*getNeedPermissions())
                    .onForwardToSettings { scope, deniedList ->
                        if (System.currentTimeMillis() - startTime < 200) {
                            scope.showForwardToSettingsDialog(
                                deniedList,
                                "${content},需要在设置中允许: ${getPermissionStr()}权限",
                                "好的",
                                "不了"
                            )
                        } else {
                            kotlin.runCatching { it.resume(value = false) }
                        }
                    }
                    .request { allGranted, _, _ ->
                        if (allGranted) {
                            KLog.e("允许了权限!")
                            kotlin.runCatching { it.resume(true) }
                        } else {
                            kotlin.runCatching {
                                it.resume(false)
                            }
                            KLog.e("未授权权限，部分功能不能使用")
                        }
                    }
            }, cancel = { kotlin.runCatching { it.resume(value = false) } })
            .showInQueue()
    }
}
fun PerInterface.checkPermissions(context: Context):Boolean=getNeedPermissions().all { PermissionX.isGranted(context, it) }

suspend fun PerInterface.checkWithReqNoDialog(activity: FragmentActivity): Boolean {
    if (getNeedPermissions().all { PermissionX.isGranted(activity, it) }) {
        return true
    }
    val title = getTitle()
    val content = getContent()
    return suspendCoroutine<Boolean> {
        val startTime = System.currentTimeMillis()
        PermissionX.init(activity)
            .permissions(*getNeedPermissions())
            .onForwardToSettings { scope, deniedList ->
                if (System.currentTimeMillis() - startTime < 200) {
                    scope.showForwardToSettingsDialog(
                        deniedList,
                        "${content},需要在设置中允许: ${getPermissionStr()}权限",
                        "好的",
                        "不了"
                    )
                } else {
                    kotlin.runCatching { it.resume(value = false) }
                }
            }
            .request { allGranted, _, _ ->
                if (allGranted) {
                    KLog.e("允许了权限!")
                    kotlin.runCatching { it.resume(true) }
                } else {
                    kotlin.runCatching {
                        it.resume(false)
                    }
                    KLog.e("未授权权限，部分功能不能使用")
                }
            }
    }
}